<?php
include_once("../includes/page_utf_class.php");
include_once("../module/product/includes/plugin_order_class.php");
include("../lang/cn/company_type_config.php");
include_once WEBROOT.'/module/inventory/config.inc.php';
$inventory_config = new config();
$order = new order;
//=======================
// if(isset($_GET['orderid'])&&is_numeric($_GET['orderid']))
// {
// 	$order->set_order_statu($_GET['orderid'],4,$_SESSION['ADMIN_USER']);
// 	msg("module.php?m=product&s=order.php");
// }
if($_GET['code'])
{
	$str=" and  a.order_id like '$_GET[code]%' ";
}
//----------------------分仓
$general_shop =$order->get_general_shop();

if ($general_shop!=0&&$general_shop!=$_GET['stock_shop']&&isset($_GET['stock_shop'])){
	msg('noright.php');
	exit;
}
if ($general_shop){
	$_GET['stock_shop'] =$general_shop;
}
if ($_GET['stock_shop']){
	$str.=" and d.stock_shop = '$_GET[stock_shop]' ";
}
//-----------------
if($_GET['stime'])
{
    $str.=" and IFNULL(a.create_time,0) >= '".$_GET['stime']."' ";
}
if($_GET['etime'])
{
    $str.=" and IFNULL(a.create_time,0) <= '".$_GET['etime']."' ";
}

if(!$_GET['stime'] && !$_GET['etime']){
    $str.=" and IFNULL(a.create_time,0) >= '".date('Y-m')."' ";
}

// $sql="select * from ".ORDER."  where status=2 $str order by id desc";
$exportSql=$sql="select sum(a.num) as total_num,b.pid,a.product_id,c.barcode,a.pic,a.product_name,a.property,b.dis_price,b.price,c.stocks,c.canbuyout,b.buy_limit,b.stock_shop,c.pricebuy,c.min_realpricebuy,c.avg_realpricebuy,c.currency_unit,a.create_time from ".ORPRO." a left join ".SETMEAL." b on a.product_id=b.id left join haiou_stock_products c on a.product_id=c.product_sku_id left join ".ORDER." d on a.order_id=d.order_id where d.status>=2 {$str} group by a.product_id order by total_num desc";
//=============================
$page = new Page;
$page->listRows=20;
if (!$page->__get('totalRows')){
	$db->query($sql);
	$de['count']=$page->totalRows = $db->num_rows();
}
$de['count']=$de['count']?$de['count']:$_GET['totalRows'];
$sql .= "  limit ".$page->firstRow.",".$page->listRows;
$de['page'] = $page->prompt();
//=============================
$db->query($sql);
$ss=$db->getRows();
$de["list"]=$ss;

if($_POST['act']=='export_sell'){//导出数据
    $db->query($exportSql);
    $export_data_temp=$db->getRows();
    
    $export_data = array();
    foreach ($export_data_temp as $k=>$v)
    {
        $export_data[] = array(
            $v['pid'],
            $v['product_id'],
            $v['barcode'],
            $v['pic'],
            $v['product_name'],
            $v['property'],
            $v['dis_price'],
            $v['price'],
            $v['min_realpricebuy'].' '.$v['currency_unit'],
            $v['avg_realpricebuy'].' '.$v['currency_unit'],
            $v['pricebuy'].' '.$v['currency_unit'],
            $v['stocks'],
            $v['canbuyout'],
            $v['buy_limit'],
            $v['total_num']
        );
    }
    
    
    include_once '../lib/php_excel.php';
    
    $filename =   "产品销量统计" . date ( "YmdHis" );
    $xls = new Excel_XML ( 'UTF-8', false );
    
    $queue[0] = array("商品ID","SKU_ID","条码code","商品图片","商品名称","商品规格","分销价(￥)","零售价(￥)","最低采购价","平均采购价","供应价","实际库存","虚拟库存","限购数量","销售数量");
    $export_data = array_merge($queue,$export_data);
    $xls->addArray ( $export_data );
    $xls->addSheet ( '销量排行' );
    
    $xls->generateXML ( $filename, true );
    exit ();
}

//----------------------分仓
$stock_shops=$order->get_stock_shops();
$tpl->assign("stock_shops",$stock_shops);
$tpl->assign("general_shop",$general_shop);
//-----------------------------

$tpl->assign("de",$de);
$tpl->assign("config",$config);
$tpl->display("product_sell_amount.htm");

?>